/**
 * 
 */
package com.example.queue.util;

import static org.hornetq.integration.transports.netty.TransportConstants.PORT_PROP_NAME;

import java.util.HashMap;
import java.util.Map;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.hornetq.api.core.TransportConfiguration;
import org.hornetq.api.jms.HornetQJMSClient;
import org.hornetq.core.config.impl.FileConfiguration;
import org.hornetq.core.server.HornetQServer;
import org.hornetq.core.server.HornetQServers;
import org.hornetq.integration.transports.netty.NettyConnectorFactory;
import org.hornetq.jms.server.JMSServerManager;
import org.hornetq.jms.server.impl.JMSServerManagerImpl;

/**
 * @author sunaina.sharma
 * 
 */
public class JMSUtil {

	static FileConfiguration configuration = null;
	static JMSServerManager jmsServerManager = null;
	static {

		try {
			configuration = new FileConfiguration();
			configuration.setConfigurationUrl("hornetq-configuration.xml");
			configuration.start();
			HornetQServer server = HornetQServers
					.newHornetQServer(configuration);
			jmsServerManager = new JMSServerManagerImpl(server,
					"hornetq-jms.xml");
			jmsServerManager.setContext(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method will start HornetQserver
	 */
	public static void startJMServer() throws Exception {

		try {
			jmsServerManager.start();
			System.out.println("STARTED::");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method will stop HornetQserver
	 */
	public static void stopJMServer() throws Exception {

		try {
			jmsServerManager.stop();
			System.out.println("STOPED::");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * This method will stop HornetQserver
	 */
	public static void sendMessage() throws Exception {
		Connection connection = null;
		try {
			// Step 1. Directly instantiate the JMS Queue object.
			Queue queue = HornetQJMSClient.createQueue("exampleQueue");

			// Step 2. Instantiate the TransportConfiguration object which
			// contains the knowledge of what transport to use,
			// The server port etc.

			Map<String, Object> connectionParams = new HashMap<String, Object>();
			connectionParams.put(PORT_PROP_NAME, 5445);

			TransportConfiguration transportConfiguration = new TransportConfiguration(
					NettyConnectorFactory.class.getName(), connectionParams);

			// Step 3 Directly instantiate the JMS ConnectionFactory object
			// using that TransportConfiguration
			ConnectionFactory cf = HornetQJMSClient
					.createConnectionFactory(transportConfiguration);

			// Step 4.Create a JMS Connection
			connection = cf.createConnection();

			// Step 5. Create a JMS Session
			Session session = connection.createSession(false,
					Session.AUTO_ACKNOWLEDGE);

			// Step 6. Create a JMS Message Producer
			MessageProducer producer = session.createProducer(queue);

			// Step 7. Create a Text Message
			TextMessage message = session
					.createTextMessage("Employee has been added in database");

			System.out.println("Employee has been added in database");

			// Step 8. Send the Message
			producer.send(message);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
